import com.google.appengine.api.datastore.*

import groovy.json.*
import groovy.time.*

def reportJSON = context.getResource("/json/izmir-earthquake-scenario-reports.json")
def categoriesJSON = context.getResource("/json/categories.json")

def slurper = new JsonSlurper()
def categories = slurper.parse(categoriesJSON)
def catMap = [:]

categories.each {
		catMap.put(it.categoryId,it.name)
}

//System.out.println(catMap)

def reports = slurper.parse(reportJSON)

reports.each {
	
	def reportDate = Date.parse("yyy-MM-dd'T'HH:mm:ss.SSS",it.reportTimestamp);
	def img = it.media.reportImages[0]?.url as Link
	new Report (
		reportID:  it.reportID,
		reportTitle: it.reportTitle,
		reportText: it.reportText,
		priorityLevel: it.priorityLevel,
		credibilityLevel: it.credibilityLevel,
		validationStatus: it.validationStatus,
		reportTimestamp: reportDate,
		clearanceLevel: it.clearanceLevel,
		reportCategoryID: it.reportCategoryID,
		reportCategoryName: catMap.get(it.reportCategoryID),
		reportImageLink: img,
		reportRead: it.reportRead,
		reportLocation: [it.reportLocation.lat.toFloat(), it.reportLocation.lng.toFloat() ] as GeoPt,		
		//lat: it.reportLocation.lat,
		//lng: it.reportLocation.lng		
		).save()	
}

//System.out.println("Refreshing data, report count " +  Report.count()  )
def  g= Report.get( 'izmir007')
//System.out.println("report title  $g.reportTitle time: $g.reportTimestamp category $g.reportCategoryID catName $g.reportCategoryName lat: $g.lat geopt $g.reportLocation")
//System.out.println("longitude " +  g.reportLocation.getLongitude()  )
println "updated datastore"


